
package com.mycompany.delivery.tableModels;

import com.mycompany.delivery.dto.ContactDTO;
import com.mycompany.delivery.dto.UserDTO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.AbstractListModel;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author Jana  Nemcova
 */
public class CourierTableModel extends AbstractTableModel {
  
    private List<UserDTO> users;
    RestTemplate restTemplate = new RestTemplate();
    //private java.util.ResourceBundle bundle;
 
    public  CourierTableModel() {
       users = new ArrayList<UserDTO>(); 
     //   bundle = java.util.ResourceBundle.getBundle("Locales/localization");
    }
    
    @Override
    public int getRowCount() {
        return users.size();
    }
 
    
    @Override
    public int getColumnCount() {
        return 1;
    }
 
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        UserDTO u = users.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return u.getLogin();
            default:
                throw new IllegalArgumentException("error with column index");
        }
    }
    
    public UserDTO getContactAt(int index){
        return users.get(index);
    }
    
    public void initUsers() {
        InitUserWorker initUserWorker = new InitUserWorker();
        initUserWorker.execute();
    }
    /*
    public void addContact(ContactDTO contact) {
        AddContactWorker addContactWorker = new AddContactWorker();
        addContactWorker.setContact(contact);
        addContactWorker.execute();
    }

    public void updateContact(ContactDTO contact) {
        UpdateContactWorker updateContactWorker = new UpdateContactWorker();
        updateContactWorker.setContact(contact);
        updateContactWorker.execute();
    }
    
    public void deleteContact(ContactDTO contact) {
        DeleteContactWorker deleteContactWorker = new DeleteContactWorker();
        deleteContactWorker.setContact(contact);
        deleteContactWorker.execute();
    }
    */
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
         //   case 1:
         //       return String.class;
            default:
                throw new IllegalArgumentException("error with column class");
        }
    }
    
    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "LOGIN";
       /*     case 1:
                return "NAME";
            case 2:
                return "SURNAME";
            case 3:
                return "EMAIL";
            case 4:
                return "CITY";
            case 5:
                return "STREET";
            case 6:
                return "ZIP";
            case 7:
                return "PHONE";*/
            default:
                throw new IllegalArgumentException("error with column name");
        }
    }
    
    private class InitUserWorker extends SwingWorker<List<UserDTO>,Void> {
        
        @Override    
        protected List<UserDTO> doInBackground() throws Exception {
            List<UserDTO> listOfCont = new ArrayList<UserDTO>();
           try {
            UserDTO[] users = restTemplate.getForObject("http://localhost:8080/pa165/deliveries/getCouriersJson", UserDTO[].class);
            for (UserDTO u : users) {
                listOfCont.add(u);
            }
        } catch (HttpClientErrorException e) {
            System.err.println(e.getMessage());
            }
                catch (ResourceAccessException ex)
         {
          //NEJDE WEB, JIŽ OZNÁMENO
             
         }  
           return listOfCont;
        }
        
        @Override
        protected void done(){
            try {
                users.clear();
                for(UserDTO u : get()){
                    users.add(u);
                }
                fireTableChanged(null);
            } catch (ExecutionException ex) {
                throw new RuntimeException(ex);
            } catch (InterruptedException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }
    /*
    private class AddContactWorker extends SwingWorker<Void,Void> {
        
        private ContactDTO contact;

        public void setContact(ContactDTO contact) {
            this.contact = contact;
        }
        
        @Override    
        protected Void doInBackground() throws Exception {
            try {
            contact = restTemplate.postForObject("http://localhost:8080/pa165/contacts/update", contact, ContactDTO.class);
        } catch (HttpClientErrorException e) {
          
            System.err.println(e.getMessage());
          }
            return null;
        }
        
        @Override
        protected void done(){
            contacts.add(contact);
            fireTableChanged(null);
        }
    }
    */
    /*
    private class UpdateContactWorker extends SwingWorker<Void,Void> {
        
        private ContactDTO contact;

        public void setContact(ContactDTO contact) {
            this.contact = contact;
        }
        
        @Override    
        protected Void doInBackground() throws Exception {
            try {
            contact = restTemplate.postForObject("http://localhost:8080/pa165/contacts/update", contact, ContactDTO.class);
        } catch (HttpClientErrorException e) {
          
            System.err.println(e.getMessage());
          }
            return null;
        }
        @Override    
        protected void done() {
            for(ContactDTO con: contacts){
                if(con.getId() == contact.getId()){
                    con = contact;
                    break;
                }
            }
            fireTableChanged(null);
        }
    }
    
    private class DeleteContactWorker extends SwingWorker<Void,Void> {
        
        private ContactDTO contact;

        public void setContact(ContactDTO contact) {
            this.contact = contact;
        }
        
        @Override    
        protected Void doInBackground() throws Exception {
            try {
            restTemplate.postForObject("http://localhost:8080/pa165/contacts/delete", contact, ContactDTO.class);
        } catch (HttpClientErrorException e) {
          
            System.err.println(e.getMessage());
          }
            return null;
        }
    
        @Override    
        protected void done() {
            contacts.remove(contact);
            fireTableChanged(null);
        }
    }*/
}
